/**
 * Created by Jie on 2017/2/5.
 */
var data = require('../../util/result');
var Model = require('../../model/core/department');

var index = {page:'core/department',title:''};
var form = {page:'core/_form/_department_form',title:''};

exports.index = function (req,res) {
    res.render(index.page, {title: index.title});
};

exports.form = function (req,res) {
    var model = new Model();
    model.one(req.params.id,function (err,result) {
        if(err){
            res.render(form.page, {title: form.title});
            return;
        }
        res.render(form.page, {title: form.title,obj:result});
    })

};

exports.one = function (req,res) {
    var model = new Model();
    model.one(req.params.id,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if((typeof result) == 'object'){
            res.json(data.result(0,"",result));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    })
}

exports.insert = function (req,res) {
    var model = new Model();
    model.insert(req.body,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if(result.insertId > 0){
            res.json(data.result(0,"数据操作成功"));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

exports.edit = function (req,res) {
    var model = new Model();
    req.body.id = req.params.id;
    model.edit(req.body,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if(result.affectedRows > 0){
            res.json(data.result(0,"数据操作成功"));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

exports.del = function (req,res) {
    var model = new Model();
    req.body.id = req.params.id;
    model.del(req.body,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if(result.affectedRows > 0){
            res.json(data.result(0,"数据操作成功"));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

exports.list = function (req,res) {
    var model = new Model();
    var where = req.body;
    model.list(where,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if((typeof result) == 'object'){
            res.json(result);
        }else{
            res.json(data.result(1,"数据异常"));
        }
    });
}

exports.all = function (req,res) {
    var model = new Model();
    var cond = {where:'',order:''};
    model.one(cond,function (err,result) {
        if(err){
            res.json(data.result(1,"数据异常"));
            return;
        }
        if((typeof result) == 'object'){
            res.json(data.result(0,"",result));
        }else{
            res.json(data.result(1,"数据异常"));
        }
    })
}